#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using FreeOnlineApplication.Class.Entity;
using FreeOnlineApplication.Class.Repository;

#endregion

namespace FreeOnlineApplication
{
    public partial class CheckInUserControl : UserControl, IChildForm, IMasterSearchCustomer, IMasterSearchBooking
    {
        CheckInRepository m_rep;
        BookingRepository m_repBooking;
        CustomerRepository m_repCustomer;
        RoomMasterRepository m_repRoomMaster;
        RoomTypeRepository m_repRoomType;
        CheckIn m_subject = new CheckIn();
        IMainForm m_mainForm;

        public CheckInUserControl(string name, IMainForm mainform)
        {
            InitializeComponent();
            m_rep = new CheckInRepository(mainform.Connection);
            m_repBooking = new BookingRepository(mainform.Connection);
            m_repCustomer = new CustomerRepository(mainform.Connection);
            m_repRoomMaster = new RoomMasterRepository(mainform.Connection);
            m_repRoomType = new RoomTypeRepository(mainform.Connection);

            InitializeButtonClick();
            m_mainForm = mainform;
            InitializeDataSource();
            this.Name = name;
            this.Dock = DockStyle.Fill;
            //loadRecords();
            ClearForm();
            setEditMode(EditMode.New);
        }

        private void InitializeDataSource()
        {
            comboBoxCustomer.DataSource = m_repCustomer.GetAll(m_mainForm.CurrentUnit.ID);
            comboBoxRoomMaster.DataSource = m_repRoomMaster.GetAll(m_mainForm.CurrentUnit.ID);
            comboBoxRoomType.DataSource = m_repRoomType.GetAll(m_mainForm.CurrentUnit.ID);
            ArrayList gned = new ArrayList();
            gned.Add("OPEN");
            gned.Add("CLOSE");
            gned.Add("CANCEL");
            comboBoxStatus.DataSource = gned;
        }

        private void InitializeButtonClick()
        {
            toolBarButtonSave.Click += new EventHandler(buttonSave_Click);
            toolBarButtonEdit.Click += new EventHandler(buttonEdit_Click);
            toolBarButtonDelete.Click += new EventHandler(buttonDelete_Click);
            toolBarButtonClear.Click += new EventHandler(buttonClear_Click);
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            InitializeDataSource();
            //loadRecords();
            ClearForm();
            setEditMode(EditMode.New);
        }
        public void ClearForm()
        {
            textBoxCode.Text = "CI" + (m_rep.GetCount(m_mainForm.CurrentUnit.ID) + 1).ToString().PadLeft(3, '0');
            textBoxNote.Text = "";
            comboBoxCustomer.SelectedIndex = -1;
            comboBoxRoomMaster.SelectedIndex = -1;
            numericUpDownDiscount.Value = 0;
            dateTimePickerStart.Value = DateTime.Today;
            dateTimePickerEnd.Value = DateTime.Today;
            comboBoxStatus.Text = "OPEN";
            dateTimePickerDate.Value = DateTime.Today;
            textBoxBookingCode.Text = "";

            m_subject = new CheckIn();
            errorProvider.SetError(textBoxCode, "");
            errorProvider.SetError(textBoxNote, "");
        }
        public void setEnableForm(bool enable)
        {
            textBoxCode.Enabled = enable;
            textBoxNote.Enabled = enable;
            comboBoxCustomer.Enabled = enable;
            comboBoxRoomMaster.Enabled = enable;
            numericUpDownDiscount.Enabled = enable;
            dateTimePickerEnd.Enabled = enable;
            dateTimePickerStart.Enabled = enable;
            comboBoxStatus.Enabled = enable;
            dateTimePickerDate.Enabled = enable;
            buttonSearchCustomer.Enabled = enable;
            buttonSearchBooking.Enabled = enable;
        }
        public bool Valid()
        {
            bool a = textBoxCode.Text == "";
            bool b = comboBoxCustomer.SelectedItem == null;
            bool c = comboBoxRoomMaster.SelectedItem == null;

            if (a) errorProvider.SetError(textBoxCode, "Can not Empty");
            if (b) errorProvider.SetError(comboBoxCustomer, "Customer can not Empty");
            if (c) errorProvider.SetError(comboBoxRoomMaster, "Room can not Empty");
            return !a && !b && !c;
        }
        private void setEditMode(EditMode editmode)
        {
            toolBarButtonSave.Enabled = editmode == EditMode.New || editmode == EditMode.Update;
            toolBarButtonEdit.Enabled = editmode == EditMode.View;
            toolBarButtonDelete.Enabled = editmode == EditMode.View;
            LoadEnableButton();
        }
        //private void loadRecords()
        //{
        //    listView1.Items.Clear();
        //    IList records = m_rep.GetAll(m_mainForm.CurrentUnit.ID);
        //    foreach (CheckIn d in records)
        //    {
        //        ListViewItem item = listView1.Items.Add(d.CODE);
        //       // item.SubItems.Add(d.NAME);
        //        //item.SubItems.Add(d.SERVICE_PRICE.ToString(Tools.DECIMAL_FORMAT));
        //        item.Tag = d;
        //    }
        //}
        private void removeRecord(int id)
        {
            foreach (ListViewItem item in listView1.Items)
            {
                CheckIn dep = (CheckIn)item.Tag;
                if (dep.ID == id)
                {
                    listView1.Items.Remove(item);
                    break;
                }
            }
        }
        private void UpdateEntity()
        {
            m_subject.CODE = textBoxCode.Text.Trim();
            m_subject.CUSTOMER = (Customer)comboBoxCustomer.SelectedItem;
            m_subject.ROOM_MASTER = (RoomMaster)comboBoxRoomMaster.SelectedItem;
            m_subject.DISCOUNT = Convert.ToDouble(numericUpDownDiscount.Value);
            m_subject.START_DATE_TIME = dateTimePickerStart.Value;
            m_subject.END_DATE_TIME = dateTimePickerEnd.Value;
            m_subject.STATUS = comboBoxStatus.Text;
            m_subject.NOTE = textBoxNote.Text;
            m_subject.UNIT = m_mainForm.CurrentUnit;
            m_subject.USER = m_mainForm.CurrentUser;
            m_subject.DATE = dateTimePickerDate.Value;

            Booking b = (Booking)textBoxBookingCode.Tag;
            m_subject.BOOKING = b;
        }
        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (Valid())
            {
                UpdateEntity();
                if (m_subject.ID == 0)
                {
                    m_rep.Insert(m_subject);
                }
                else
                {
                    m_rep.Update(m_subject);
                }
                MessageBox.Show("Data telah tersimpan.");
                setEditMode(EditMode.Update);
                setEnableForm(false);
                //ClearForm();
                //loadRecords();
            }
        }
        private void buttonEdit_Click(object sender, EventArgs e)
        {
            setEnableForm(true);
            setEditMode(EditMode.Update);
        }
        private void buttonClear_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Are sure to Clear?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, new EventHandler(clear));
        }
        private void clear(object obj, EventArgs e)
        {
            MessageBoxWindow w = (MessageBoxWindow)obj;
            if (w.DialogResult == DialogResult.Yes)
            {
                ClearForm();
                setEnableForm(true);
                setEditMode(EditMode.New);
            }
        }
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Delete record?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, new EventHandler(delete));
        }
        private void delete(object obj, EventArgs e)
        {
            try
            {
                MessageBoxWindow w = (MessageBoxWindow)obj;
                if (w.DialogResult == DialogResult.Yes)
                {
                    if (m_subject.ID > 0)
                    {
                        m_rep.Delete(m_subject);
                        removeRecord(m_subject.ID);
                        ClearForm();
                        setEnableForm(true);
                        setEditMode(EditMode.New);
                    }
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) return;
            ClearForm();
            m_subject = (CheckIn)listView1.SelectedItem.Tag;
            loadData(m_subject);
            setEnableForm(false);
            setEditMode(EditMode.View);
        }
        private void loadData(CheckIn emp)
        {
            emp.CUSTOMER = m_repCustomer.GetById(emp.CUSTOMER.ID);
            emp.ROOM_MASTER = m_repRoomMaster.GetById(emp.ROOM_MASTER.ID);
            emp.ROOM_MASTER.ROOM_TYPE = m_repRoomType.GetById(emp.ROOM_MASTER.ROOM_TYPE.ID);

            textBoxCode.Text = emp.CODE;
            textBoxNote.Text = emp.NOTE;

            comboBoxCustomer.Text = emp.CUSTOMER.ToString();
            comboBoxCustomer_SelectedIndexChanged(null, null);

            comboBoxRoomType.Text = emp.ROOM_MASTER.ROOM_TYPE.ToString();
            comboBoxRoomType_SelectedIndexChanged(null, null);
            
            comboBoxRoomMaster.Text = emp.ROOM_MASTER.ToString();
            comboBoxRoomMaster_SelectedIndexChangedQueued(null, null);

            numericUpDownDiscount.Value = Convert.ToDecimal(emp.DISCOUNT);
            dateTimePickerStart.Value = emp.START_DATE_TIME;
            dateTimePickerEnd.Value = emp.END_DATE_TIME;
            comboBoxStatus.Text = emp.STATUS;
            dateTimePickerDate.Value = emp.DATE;
            textBoxBookingCode.Text = "";

            if (emp.BOOKING != null)
            {
                emp.BOOKING = m_repBooking.GetById(emp.BOOKING.ID);
                textBoxBookingCode.Text = emp.BOOKING == null ? "" : emp.BOOKING.CODE;
            }
            
        }

        #region IMainMenu Members

        public void Save()
        {
            buttonSave_Click(null, null);
        }

        public void New()
        {
            buttonClear_Click(null, null);
        }

        public void Delete()
        {
            buttonDelete_Click(null, null);
        }

        public void Edit()
        {
            buttonEdit_Click(null, null);
        }

        public void LoadEnableButton()
        {
            m_mainForm.SetEnableMenuButton(toolBarButtonSave.Enabled, toolBarButtonClear.Enabled,
            toolBarButtonDelete.Enabled, toolBarButtonEdit.Enabled);
        }

        #endregion

        private void comboBoxCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {
            Customer c = (Customer)comboBoxCustomer.SelectedItem;
            textBoxIDNO.Text = "";
            textBoxName.Text = "";
            textBoxAddress.Text = "";
            textBoxPhone.Text = "";
            if (c == null) return;
            textBoxIDNO.Text = c.ID_NO;
            textBoxName.Text = c.NAME;
            textBoxAddress.Text = c.ADDRESS;
            textBoxPhone.Text = c.PHONE;
        }

        private void comboBoxRoomType_SelectedIndexChanged(object sender, EventArgs e)
        {
            RoomType c = (RoomType)comboBoxRoomType.SelectedItem;
            textBoxFacility.Text = "";
            comboBoxRoomMaster.DataSource = null;
            if (c == null) return;
            textBoxFacility.Text = c.FACILITY;
            comboBoxRoomMaster.DataSource = m_repRoomMaster.GetAllByRoomType(c.ID);

        }

        private void comboBoxRoomMaster_SelectedIndexChangedQueued(object sender, EventArgs e)
        {
            RoomMaster c = (RoomMaster)comboBoxRoomMaster.SelectedItem;
            numericUpDownTariff.Value = 0;
            if (c == null) return;
            numericUpDownTariff.Value = Convert.ToDecimal(c.TARIFF);
        }

        private void buttonSearchCustomer_Click(object sender, EventArgs e)
        {
            CustomerSearchForm sch = new CustomerSearchForm(m_mainForm, this);
            sch.ShowDialog();
        }
        public void UpdateCustomerBySearch(Customer cus)
        {
            comboBoxCustomer.DataSource = m_repCustomer.GetAll(m_mainForm.CurrentUnit.ID);
            if (cus == null) return;
            comboBoxCustomer.Text = cus.ToString();
            comboBoxCustomer_SelectedIndexChanged(null, null);
        }

        private void buttonCheckAvailableRoom_Click(object sender, EventArgs e)
        {
            AvailableRoomForm frm = new AvailableRoomForm(m_mainForm);
            frm.ShowDialog();
        }
        public void LoadByBooking(Booking booking)
        {
            booking.CUSTOMER = m_repCustomer.GetById(booking.CUSTOMER.ID);
            booking.ROOM_MASTER = m_repRoomMaster.GetById(booking.ROOM_MASTER.ID);
            booking.ROOM_MASTER.ROOM_TYPE = m_repRoomType.GetById(booking.ROOM_MASTER.ROOM_TYPE.ID);

            textBoxBookingCode.Text = booking.CODE;
            textBoxNote.Text = booking.NOTE;

            comboBoxCustomer.Text = booking.CUSTOMER.ToString();
            comboBoxCustomer_SelectedIndexChanged(null, null);

            comboBoxRoomType.Text = booking.ROOM_MASTER.ROOM_TYPE.ToString();
            comboBoxRoomType_SelectedIndexChanged(null, null);

            comboBoxRoomMaster.Text = booking.ROOM_MASTER.ToString();
            comboBoxRoomMaster_SelectedIndexChangedQueued(null, null);

            numericUpDownDiscount.Value = Convert.ToDecimal(booking.DISCOUNT);
            dateTimePickerStart.Value = booking.START_DATE_TIME;
            dateTimePickerEnd.Value = booking.END_DATE_TIME;
            comboBoxStatus.Text = booking.STATUS;
            dateTimePickerDate.Value = booking.DATE;

            textBoxBookingCode.Tag = booking;
        }

        private void buttonSearchBooking_Click(object sender, EventArgs e)
        {
            BookingSearchForm searchBookingFrm = new BookingSearchForm(m_mainForm, this);
            searchBookingFrm.ShowDialog();
        }

        private void buttonFind_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            IList records = m_rep.GetAllSearch(textBox1.Text.Trim(), m_mainForm.CurrentUnit.ID);
            if (records.Count == 0)
            {
                ListViewItem item = listView1.Items.Add("Data tidak di temukan");
            }
            foreach (CheckIn d in records)
            {
                d.CUSTOMER = m_repCustomer.GetById(d.CUSTOMER.ID);
                d.ROOM_MASTER = m_repRoomMaster.GetById(d.ROOM_MASTER.ID);
                ListViewItem item = listView1.Items.Add(d.CODE);
                item.SubItems.Add(d.CUSTOMER.ID_NO);
                item.SubItems.Add(d.CUSTOMER.NAME);
                item.SubItems.Add(d.ROOM_MASTER.CODE);
                item.SubItems.Add(d.START_DATE_TIME.ToString("dd-MM-yyyy HH:mm"));
                item.SubItems.Add(d.END_DATE_TIME.ToString("dd-MM-yyyy HH:mm"));
                item.SubItems.Add(d.STATUS);
                item.Tag = d;
            }
        }
    }
}